package com.fjt.book_pay.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.fjt.book_pay.dao.P_UserDao;
import com.fjt.book_pay.pojo.Bank;
import com.fjt.book_pay.pojo.P_Record;
import com.fjt.book_pay.pojo.P_User;
import com.fjt.book_pay.util.SendEmail;

@Service("p_userService")
public class P_UserService {
//	public ApplicationContext context=new ClassPathXmlApplicationContext("spring-mybatis.xml");
//	private IUserDao userDao=context.getBean("IUserDao",IUserDao.class);
	private P_UserDao p_userDao;
	private P_User p_user;
	private BankService bankService;
	private P_RecordService p_RecordService;
	
	public P_UserDao getP_userDao() {
		return p_userDao;
	}
	public void setP_userDao(P_UserDao p_userDao) {
		this.p_userDao = p_userDao;
	}
	public P_User getP_user() {
		return p_user;
	}
	public void setP_user(P_User p_user) {
		this.p_user = p_user;
	}
	public BankService getBankService() {
		return bankService;
	}
	@Resource(name="bankservice")
	public void setBankService(BankService bankService) {
		this.bankService = bankService;
	}
	public P_RecordService getP_RecordService() {
		return p_RecordService;
	}
	@Resource(name="p_recordservice")
	public void setP_RecordService(P_RecordService p_RecordService) {
		this.p_RecordService = p_RecordService;
	}
	public P_User getUser() {
		return p_user;
	}
	@Resource(name="p_user")
	public void setUser(P_User p_user) {
		this.p_user = p_user;
	}
	public P_UserDao getUserDao() {
		return p_userDao;
	}
	
	@Resource
	public void setUserDao(P_UserDao p_userDao) {
		this.p_userDao = p_userDao;
	}
	
	public Map<String, Object> sendmailcode(P_User p_user,HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		Integer code=(int) (8999*Math.random()+1000);
		String toEmail=p_user.getPay_email();
		HttpSession session=request.getSession();
		if(toEmail!=null){
			SendEmail.send(toEmail,code.toString());
			session.setAttribute("code", code);
			session.setMaxInactiveInterval(900);
			System.out.println(session.getId());
			System.out.println(code);
			System.out.println(toEmail);
			map.put("code", 1);
			map.put("result", "获取验证码成功！");
		}else{
			map.put("code", 0);
			map.put("result", "邮箱不存在！");
		}
		return map;
	}
	
	public Map<String, Object> getUserInfo(P_User p_user){
		Map<String, Object> map=new HashMap<String, Object>();
		P_User p_User=p_userDao.findUserInfoByName(p_user);
		if(p_User!=null){
			System.out.println("查询成功！");
			map.put("code", 1);
			map.put("result", "查询成功！");
			map.put("user_infor", p_User);
		}else{
			System.out.println("查询失败");
			map.put("code", 0);
			map.put("result", "用户不存在！");
		}
		return map;
	}
	
	public boolean validate(P_User p_User){//验证用户
		if(p_userDao.validateUser(p_User)!=null){
			return true;
		}else{
			return false;
		}
		
	}
	public Map<String,Object> validateUser(P_User p_User,HttpServletRequest request){//验证用户，返回结果
		Map<String,Object> map = new HashMap<String,Object>();
		if(validate(p_User)){
			map.put("code", 1);
			map.put("result", "登录成功！");
			request.getSession().setAttribute("username", p_User.getPay_name());
		}else{
			map.put("code", 0);
			map.put("result", "用户名或密码错误！");
		}
			
		return map;
	}
	
	public Map<String,Object> addP_User(P_User p_user){//添加用户
		Map<String,Object> map = new HashMap<String,Object>();
//		int id = (int)(Math.random()*50000)+(int)(Math.random()*50000);
		System.out.println(p_user.getPay_email());
		if(p_userDao.findUserInfoByName(p_user)==null){
			p_userDao.addUser(p_user);
			map.put("code", 1);
			map.put("result", "注册成功");
		}else{
			map.put("code", 0);
			map.put("result", "用户已经存在！");
		}
			
		return map;
	}
	
	public Map<String,Object> resetPassWord(P_User p_user){//更改用户密码
		Map<String,Object> map = new HashMap<String,Object>();
		if(p_userDao.resetPassword(p_user)){
			map.put("code", 1);
			map.put("result", "修改成功！");
		}else{
			map.put("code", 0);
			map.put("result", "修改密码失败！");
		}
			
		return map;
	}
	public Map<String,Object> modifyPassWord(P_User p_user){//更改用户密码
		Map<String,Object> map = new HashMap<String,Object>();
		if(p_userDao.modifyPassword(p_user)){
			map.put("code", 1);
			map.put("result", "修改成功！");
		}else{
			map.put("code", 0);
			map.put("result", "修改密码失败！");
		}
			
		return map;
	}
	
	public Map<String,Object> modifyUserInfo(P_User p_user,String oldname){//更改用户信息
		
		Map<String,Object> map = new HashMap<String,Object>();
		if(p_userDao.modifyUserInfo(p_user.getPay_email(),p_user.getPay_phone()
				,p_user.getBank_number(),oldname)){
			map.put("code", 1);
			map.put("result", "修改成功！");
		}else{
			map.put("code", 0);
			map.put("result", "修改信息失败！");
		}
		return map;
	}
	
	public Map<String, Object> checkPayMoney(P_User p_user){//查支付账户
		Map<String, Object> map=new HashMap<String, Object>();
		Integer paymoney=p_userDao.checkPaymoney(p_user);
		if(paymoney>=0){
			map.put("code", 1);
			map.put("result", "查询成功！");
			map.put("paymoney", paymoney);
		}else{
			map.put("code", 0);
			map.put("result", "查询失败！");
		}
		return map;
	}
	
	public Map<String, Object> checkBankMoney(P_User p_user){//查银行账户
		Bank bank=new Bank();
		bank.setBank_number(p_userDao.findUserBank(p_user));
		Integer bankmoney= bankService.checkmoney(bank);
		Map<String, Object> map=new HashMap<String, Object>();
		if(bankmoney>=0){
			map.put("code", 1);
			map.put("result", "查询成功！");
			map.put("paymoney", bankmoney);
		}else{
			map.put("code", 0);
			map.put("result", "查询失败！");
		}
		return map;
	}
	
	public Map<String, Object> FromBanktoBank(P_User from,P_User to,int money){//查支付账户
		Bank frombank=new Bank();
		frombank.setBank_number(from.getBank_number());
		Map<String, Object> map=new HashMap<String, Object>();
		P_Record p_Record=new P_Record();
		System.out.println("frombank");
		int bankmoney=bankService.checkmoney(frombank);
		if(bankmoney-money>=0){
			boolean a=bankService.subBankMoney(from.getBank_number(),from.getPay_password(), money);
			if(a==false){
				map.put("code", 0);
				map.put("result", "转账失败！");
				return map;
			}
			boolean b=bankService.addBankMoney(to.getBank_number(),money);
			if(a&b){
				p_Record.setBuyer_name(from.getPay_name());
				p_Record.setMoney(money);
				p_Record.setSeller_name(to.getPay_name());
				Date date=new Date();
				System.out.println(date);
				p_Record.setDate(date);
	//			p_Record.setDate(date);
				p_RecordService.addRecord(p_Record);
				map.put("code", 1);
				map.put("result", "转账成功！");
			}else{
				map.put("code", 0);
				map.put("result", "转账失败！");
			}
		}else{
			map.put("code", 0);
			map.put("result", "转账失败！");
		}
			
		return map;
	}
	
	public Map<String, Object> FromBanktoPay(P_User from,P_User to,int money){//查支付账户
		Bank frombank=new Bank();
		frombank.setBank_number(from.getBank_number());
		Map<String, Object> map=new HashMap<String, Object>();
		P_Record p_Record=new P_Record();
		int bankmoney=bankService.checkmoney(frombank);
		if(bankmoney-money>=0){
			boolean a=bankService.subBankMoney(from.getBank_number(),from.getPay_password(), money);
			
			System.out.println(from.getBank_number());
			System.out.println(from.getPay_password());
			System.out.println(a);
			if(a==false){
				map.put("code", 0);
				map.put("result", "转账失败！");
				return map;
			}
			boolean b=p_userDao.addPayMoney(to.getPay_name(), money);
			System.out.println(b);
			if(a&b){
				p_Record.setBuyer_name(from.getPay_name());
				p_Record.setMoney(money);
				p_Record.setSeller_name(to.getPay_name());
				Date date=new Date();
				System.out.println(date);
				p_Record.setDate(date);
	//			p_Record.setDate(date);
				p_RecordService.addRecord(p_Record);
				map.put("code", 1);
				map.put("result", "转账成功！");
			}else{
				map.put("code", 0);
				map.put("result", "转账失败0！");
			}		
		}else{
			map.put("code", 0);
			map.put("result", "转账失败1！");
		}
			
		return map;
	}
	
	public Map<String, Object> FromPaytoBank(P_User from,P_User to,int money){//查支付账户
		Map<String, Object> map=new HashMap<String, Object>();
		P_Record p_Record=new P_Record();
		if(p_userDao.checkPaymoney(from)-money>=0){
			boolean a=p_userDao.subPayMoney(from.getPay_name(),from.getPay_password(), money);
			if(a==false){
				map.put("code", 0);
				map.put("result", "转账失败！");
				return map;
			}
			boolean b=bankService.addBankMoney(to.getBank_number(), money);
			if(a&b){
				p_Record.setBuyer_name(from.getPay_name());
				p_Record.setMoney(money);
				p_Record.setSeller_name(to.getPay_name());
				Date date=new Date();
				System.out.println(date);
				p_Record.setDate(date);
	//			p_Record.setDate(date);
				p_RecordService.addRecord(p_Record);
				map.put("code", 1);
				map.put("result", "转账成功！");
			}else{
				map.put("code", 0);
				map.put("result", "转账失败0！");
			}			
		}else{
			map.put("code", 0);
			map.put("result", "转账失败1！");
		}
			
		return map;
	}
	
	public Map<String, Object> FromPaytoPay(P_User from,P_User to,int money){//查支付账户
		Map<String, Object> map=new HashMap<String, Object>();
		P_Record p_Record=new P_Record();
		if(p_userDao.checkPaymoney(from)-money>=0){
			boolean a=p_userDao.subPayMoney(from.getPay_name(),from.getPay_password(), money);
			if(a==false){
				map.put("code", 0);
				map.put("result", "转账失败！");
				return map;
			}
			boolean b=p_userDao.addPayMoney(to.getPay_name(), money);
			if(a&b){
				p_Record.setBuyer_name(from.getPay_name());
				p_Record.setMoney(money);
				p_Record.setSeller_name(to.getPay_name());
				Date date=new Date();
				System.out.println(date);
				p_Record.setDate(date);
	//			p_Record.setDate(date);
				p_RecordService.addRecord(p_Record);
				map.put("code", 1);
				map.put("result", "转账成功！");
			}else{
				map.put("code", 1);
				map.put("result", "转账失败！");
			}		
		}else{
			map.put("code", 1);
			map.put("result", "转账失败！");
		}
		return map;
	}
}
